﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartMD.DataAccess.Interface.Dao;
using System.Data.OleDb;
using System.IO;
using System.Windows.Forms;
using SmartMD.DataModel.Entity;
using System.Data;

namespace SmartMD.DataAccess.Oracle.DaoImpl
{
    public class ImageDaoImpl : IBaseGenericDao
    {
        public bool SaveOrUpdateObject(Object entity)
        {
            IndiceImage indiceImage = (IndiceImage)entity;
            string fileName = indiceImage.ImageUrl;
            //string conn_str = "Provider=OraOLEDB.Oracle.1;Password=agoodjob;Persist Security Info=True;User ID=ams;Data Source=ims;Extended Properties=";//Provider必須用OraOLEDB.Oracle.1
            XMLHelper xmlOper = new XMLHelper();
            String conn_str = xmlOper.GetXmlNodeAttribute("SmartMD.Manager.vshost.exe.config", "configuration/appSettings/add[@key='DataAddress']", "value");
            OleDbConnection cn = new OleDbConnection(conn_str);
            cn.Open();

            OleDbCommand cmd = new OleDbCommand("update DUO_IMAGE set VALUE = ? where ID='" + indiceImage.Id + "'", cn);
            cmd.Parameters.Add("VALUE", OleDbType.Binary);

            FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Read);
            byte[] imageData = new byte[fs.Length];
            fs.Read(imageData, 0, System.Convert.ToInt32(fs.Length));
            fs.Close();

            cmd.Parameters["VALUE"].Value = imageData;
            try
            {
                int result = cmd.ExecuteNonQuery();
                if (result < 1)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("insert into DUO_IMAGE (ID, KEY, VALUE)");
                    sb.Append("VALUES ('" + indiceImage.Id + "' , '' , ? )");
                    OleDbCommand cmd1 = new OleDbCommand(sb.ToString(), cn);
                    cmd1.Parameters.Add("VALUE", OleDbType.Binary);
                    cmd1.Parameters["VALUE"].Value = imageData;
                    result = cmd1.ExecuteNonQuery();
                }
                if (result >= 1)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                cn.Close();
            }
            return false;
        }

        public IndiceImage GetObjectById(string Id)
        {
            //string conn_str = "Provider=OraOLEDB.Oracle.1;Password=agoodjob;Persist Security Info=True;User ID=ams;Data Source=ims;Extended Properties=";//Provider必須用OraOLEDB.Oracle.1
            XMLHelper xmlOper = new XMLHelper();
            String conn_str = xmlOper.GetXmlNodeAttribute("SmartMD.Manager.vshost.exe.config", "configuration/appSettings/add[@key='DataAddress']", "value");
            OleDbDataAdapter da = new OleDbDataAdapter();
            DataSet ds = new DataSet();
            OleDbConnection conn = new OleDbConnection(conn_str);
            OleDbCommand cmd = new OleDbCommand("select ID, KEY, VALUE FROM  DUO_IMAGE  WHERE ID = '" + Id + "'", conn);
            da.SelectCommand = cmd;
            cmd.Connection.Open();
            da.Fill(ds);
            
            IndiceImage item = null;
          //  byte[] img = new byte[0];
            DataRow dr;
            dr = ds.Tables[0].Rows[0];

            if (!ds.Tables[0].Rows[0]["VALUE"].ToString().Equals(""))
            {
                item = new IndiceImage();
                item.Value =  (byte[])dr["VALUE"];
                item.Id = (string)dr["ID"];

            }
            conn.Close();
            return item;
        }
        public bool DeleteObject(Object entity)
        {
            return false;
        }
    }
}
